﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Articles
{
    public class Article 
    {
        public static void InsertArticle(Model.Article article,ref Model.NutriLeafDBEntities context)
        {

            context.Articles.AddObject(article);
            context.SaveChanges();

        }

        public static void InsertArticle(string articleTitle, string articleDescription, string articleText, int userId, int categoryId, bool IsApproved, ref Model.NutriLeafDBEntities context)
        {

            Model.Article ar = new Model.Article();
            ar.ArticleTitle = articleTitle;
            ar.AriticleDescription = articleDescription;
            ar.ArticleText = articleText;
            ar.UserID = userId;
            ar.CategoryID = categoryId;
            ar.Approved = IsApproved;
            context.Articles.AddObject(ar);
            context.SaveChanges();
        }

        public static void ApproveArticle(int articleId, ref Model.NutriLeafDBEntities context)
        {
            context.Articles.Where(p => p.ArticleID == articleId).First().Approved = true;
            context.SaveChanges();
        }

        public static void ApproveArticle(Model.Article article, ref Model.NutriLeafDBEntities context)
        {
            context.Articles.Where(p => p.ArticleID == article.ArticleID).First().Approved = true;
            context.SaveChanges();
        }
        
        public static bool IsApproved(int articleId, ref Model.NutriLeafDBEntities context)
        {
            return context.Articles.Where(p => p.ArticleID == articleId).First().Approved.Value;
        }

        public static void UpdateArticle(Model.Article article, ref Model.NutriLeafDBEntities context)
        {
            Model.Article ar = context.Articles.Where(p => p.ArticleID == article.ArticleID).First();
            ar = article;
            context.SaveChanges();
        }

        public static List<Model.Article> GetArticlesWrittenByUser(int userId, ref Model.NutriLeafDBEntities context)
        {
            return context.Articles.Where(p =>p.UserID==userId).ToList();
        }

        public static List<Model.Article> GetArticlesByCategory(Model.ArticleCategory category, ref Model.NutriLeafDBEntities context)
        {
            return context.Articles.Where(p => p.ArticleCategory.ArticleCategoryID == category.ArticleCategoryID).ToList();
            
        }

        public static List<Model.Article> GetArticleByCategory(int categoryId, ref Model.NutriLeafDBEntities context)
        {
            return context.Articles.Where(p => p.ArticleCategory.ArticleCategoryID == categoryId).ToList();
            
        }

        public static void InsertCategory(Model.ArticleCategory category, ref Model.NutriLeafDBEntities context)
        {
            context.ArticleCategories.AddObject(category);
            context.SaveChanges();
        }

        public static void InsertCategory(string categoryName, string categoryDescription, int ParentCategoryID, ref Model.NutriLeafDBEntities context)
        {
            Model.ArticleCategory cat = new Model.ArticleCategory();
            cat.CategoryName = categoryName;
            cat.CategoryDescription = categoryDescription;
            cat.ParentCategoryID = ParentCategoryID;
            context.ArticleCategories.AddObject(cat);
            context.SaveChanges();
        }

        public static void RemoveCategory(int categoryId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.ArticleCategories.Where(p => p.ArticleCategoryID == categoryId).First());
            context.SaveChanges();
        }

        public static void RemoveCategory(Model.ArticleCategory category, ref Model.NutriLeafDBEntities context)
        {
           context.DeleteObject(context.ArticleCategories.Where(p=>p.ArticleCategoryID==category.ArticleCategoryID).First());
            context.SaveChanges();
        }

        public static void UpdateCateogory(Model.ArticleCategory category, ref Model.NutriLeafDBEntities context)
        {
            Model.ArticleCategory cat = context.ArticleCategories.Where(p => p.ArticleCategoryID == category.ArticleCategoryID).First();
            cat = category;
            context.SaveChanges();
        }

        public static void LikeArticle(int articleId, int userId, bool like, ref Model.NutriLeafDBEntities context)
        {
            Model.ArticleLike likes = new Model.ArticleLike();
            likes.ArticleID = articleId;
            likes.UserID = userId;
            likes.DateStamp = DateTime.Now;
            likes.Liked = like;
            context.ArticleLikes.AddObject(likes);
            context.SaveChanges();
        }

        public static void LikeArticle(Model.Article article, Model.User user, bool like, ref Model.NutriLeafDBEntities context)
        {
            Model.ArticleLike likes = new Model.ArticleLike();
            likes.ArticleID = article.ArticleID;
            likes.UserID = user.UserID;
            likes.DateStamp = DateTime.Now;
            likes.Liked = like;
            context.ArticleLikes.AddObject(likes);
            context.SaveChanges();
        }

        public static void RemoveArticleLike(int likeId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.ArticleLikes.Where(p =>p.ArticleLikeID == likeId).First());
            context.SaveChanges();
        }

        public static void RemoveArticleLike(Model.ArticleLike like, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.ArticleLikes.Where(p => p.ArticleLikeID == like.ArticleLikeID).First());
            context.SaveChanges();
        }

        public static List<Model.Article> GetArticlesLikedByUser(int userId, ref Model.NutriLeafDBEntities context)
        {
            List<Model.Article> ar = new List<Model.Article>();
           List<Model.ArticleLike> l= context.ArticleLikes.Where(p => p.Liked == true && p.UserID == userId).ToList();
           foreach (Model.ArticleLike ls in l) {
               ar.Add(ls.Article);
           }
           return ar;
        }

        public static List<Model.Article> GetMostPopularArticles(int count, ref Model.NutriLeafDBEntities context)
        {
            context.Connection.Open();
            return (from a in context.Articles
                       orderby a.ArticleLikes.Count descending 
                       select a ).Take(count).ToList();

            
        }


      
    }
}